package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrEnum;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.io.IOException;
public class SubChoiceRespData extends AxdrType {
    private byte[] dataCode = null;
    private Choice choice = Choice._ERR_NONE_SELECTED;
    private int length = 0;
    private AxdrEnum dar = null;
    private SubSeqOfRecords records = null;

    public SubChoiceRespData() {
    }

    public byte[] getDataCode() {
        return dataCode;
    }

    public Choice getChoice() {
        return choice;
    }

    public int getLength() {
        return length;
    }

    public AxdrEnum getDar() {
        return dar;
    }

    public SubSeqOfRecords getRecords() {
        return records;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setDar(AxdrEnum dar) {
        resetChoices();
        this.choice = Choice.DAR;
        this.dar = dar;
    }

    public void setRecords(SubSeqOfRecords records) {
        resetChoices();
        this.choice = Choice.RECORDS;
        this.records = records;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        AxdrEnum choosen = new AxdrEnum();
        choosen.setConst();
        codeLength += choosen.decode(input);
        resetChoices();
        choice = Choice.getEnumByValue((int)choosen.getValue());
        if (choice == Choice.DAR) {
            dar = new AxdrEnum();
            dar.setConst();
            codeLength += dar.decode(input);
            return codeLength;
        } else if (choice == Choice.RECORDS) {
            records = new SubSeqOfRecords();
            records.setDataLength(length);
            codeLength += records.decode(input);
            return codeLength;
        }
        throw new Exception("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        if (dataCode != null) {
            output.writeByte(dataCode);
            return dataCode.length;
        }
        if (choice == Choice._ERR_NONE_SELECTED) {
            throw new Exception("Error encoding AxdrChoice: No item in choice was selected.");
        }
        int codeLength = 0;
        if (choice == Choice.RECORDS) {
            if (records != null) {
                codeLength += records.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(1);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("records is null");
            }
        } else if (choice == Choice.DAR) {
            codeLength += dar.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(0);
            codeLength += c.encode(output);
            return codeLength;
        }
        throw new Exception("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        encode(revOStream);
        dataCode = revOStream.getArray();
    }

    public Choice getChoiceIndex() {
        return choice;
    }

    public void resetChoices() {
        choice = Choice._ERR_NONE_SELECTED;
        dar = null;
        records = null;
    }

    public String toString() {
        if (choice == Choice.DAR) {
            if (dar != null) {
                return "choice: {dar: " + dar + "}";
            } else {
                return "choice is dar but dar is null";
            }
        } else if (choice == Choice.RECORDS) {
            if (records != null) {
                return "choice: {records: " + records + "}";
            } else {
                return "choice is records but records is null";
            }
        } else {
            return "unknown";
        }
    }

    enum Choice {
        _ERR_NONE_SELECTED(-1),
        DAR(0),
        RECORDS(1);

        private final int value;

        private Choice(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }



        /**
         * 通过传入的字符串匹配枚举，传入值
         *
         * @param value
         * @return
         */
        public static Choice getEnumByValue(int value) {
            for (Choice item : Choice.values()) {
                if (value == item.getValue()) {
                    return item;
                }
            }
            return null;
        }
    }
}